package com.edu.schoolforum.controller;

import com.edu.schoolforum.dto.*;
import com.edu.schoolforum.enums.ResultEnum;
import com.edu.schoolforum.pojo.*;
import com.edu.schoolforum.service.PostService;
import com.edu.schoolforum.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author tao
 * @date 2021/8/8 14:22
 */
@Controller
@RestController
@RequestMapping("/api")
public class PostController {
    @Autowired
    PostService postService;

   @PostMapping("/platefindPost")
    public Object findPost(@RequestBody PageRequestDto pageRequestDto){
       return postService.findPage(pageRequestDto);
   }

   @PostMapping("/indexfindPost")
   public Object mfindPost(@RequestBody MpageRequestDto mpageRequestDto){
       return postService.mfindPage(mpageRequestDto);
   }

   @GetMapping("/allPost")
    public CommonResult findAllPost(){
       List<Post> posts=postService.selectAllPost();
       return CommonResult.success(posts);
   }

   @PostMapping("/sendPost")
    public CommonResult sendPost(@RequestBody PostRequestDto postRequestDto){
       Post po=new Post();
       if(postRequestDto.getAuthorName()!=null){
           po.setAuthorName(postRequestDto.getAuthorName());
       }
       if(postRequestDto.getAvatarUrl()!=null){
           po.setAvatarUrl(postRequestDto.getAvatarUrl());
       }
       Date date=new Date();
       SimpleDateFormat s1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       po.setDate(s1.format(date));
       if(postRequestDto.getTitle()!=null){
           po.setTitle(postRequestDto.getTitle());
       }
       if(postRequestDto.getContent()!=null){
           po.setContent(postRequestDto.getContent());
       }
       if(postRequestDto.getPlate()!=null){
           po.setPlate(postRequestDto.getPlate());
       }
       if(postRequestDto.getImgsUrl()!=null){
           po.setImgsUrl(postRequestDto.getImgsUrl());
       }
       if(postRequestDto.getVideoUrl()!=null){
           po.setVideoUrl(postRequestDto.getVideoUrl());
       }
       po.setLikes(0);
       po.setComments(0);
       int i=postService.insertPost(po);
       if(i==0){
           return CommonResult.fail(ResultEnum.INSERT_POST_FAIL);
       }
       return CommonResult.success();
   }

   @PostMapping("/replyPost")
    public CommonResult replyPost(@RequestBody ReplyPostRequestDto replyPostRequestDto){
       ReplyPost replyPost=new ReplyPost();
       replyPost.setId(replyPostRequestDto.getId());
       replyPost.setRid(replyPostRequestDto.getId());
       if(replyPostRequestDto.getRavatarUrl()!=null){
           replyPost.setRavatarUrl(replyPostRequestDto.getRavatarUrl());
       }
       if(replyPostRequestDto.getRcontent()!=null){
           replyPost.setRcontent(replyPostRequestDto.getRcontent());
       }
       if(replyPostRequestDto.getRname()!=null){
           replyPost.setRname(replyPostRequestDto.getRname());
       }
       Date date=new Date();
       SimpleDateFormat s1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       replyPost.setRdate(s1.format(date));
       replyPost.setRlikes(0);
       int i=postService.insertReplyPost(replyPost);
       if(i==0){
           return CommonResult.fail(ResultEnum.REPLY_POST_FAIL);
       }
       Integer id= replyPostRequestDto.getId();
       int a=postService.CommentCountAdd(id);
       return CommonResult.success();
   }

   @GetMapping("/getReplyPost")
    public CommonResult getReplyPost(@RequestParam("id")Integer id){
       List<ReplyPost> replyPostList=postService.selectReplyPost(id);
       ReplyPost temp=null;
       for(int i=0;i<replyPostList.size();i++){
           for(int j=0;j<replyPostList.size()-i-1;j++){
               if(replyPostList.get(j).getRlikes()<replyPostList.get(j+1).getRlikes()){
                   temp=replyPostList.get(j);
                   replyPostList.set(j,replyPostList.get(j+1));
                   replyPostList.set(j+1,temp);
               }
           }
       }
       return CommonResult.success(replyPostList);
   }

   @GetMapping("/getOnePost")
    public CommonResult getOnePost(@RequestParam("id") Integer id){
       Post post=postService.selectOnePost(id);
       if(post==null){
           return CommonResult.fail(ResultEnum.FIND_POST_FAIL);
       }
       return CommonResult.success(post);
   }

   @PutMapping("/updatePostlikes")
    public CommonResult updatePostlikes(@RequestParam("id")Integer id,@RequestParam("likes")Integer likes){
       PostLikes postLikes=new PostLikes();
       postLikes.setLikes(likes);
       postLikes.setId(id);
       int i=postService.updatePostLikes(postLikes);
       if(i==0){
           return CommonResult.fail(ResultEnum.UPDATE_MESSAGE_FAIL);
       }
       return CommonResult.success();
   }

   @GetMapping("/getReplyPostCount")
    public CommonResult getReplyPostCount(@RequestParam("id")Integer id){
       int i=postService.getReplyPostCount(id);
       return CommonResult.success(i);
   }

   @PostMapping("/insertLikeMessage")
    public CommonResult insertLikeMessage(@RequestBody LikesRequestDto likesRequestDto){
       LMessage lMessage=new LMessage();
       lMessage.setUserName(likesRequestDto.getUserName());
       lMessage.setTid(likesRequestDto.getTid());
       LikeMessage likeMessage=postService.getLikeMessage(lMessage);
       if(likeMessage!=null){
           return CommonResult.fail(ResultEnum.LIKE_EXIST_FAIL);
       }else {
           LikeMessage likeMessage1 = new LikeMessage();
           likeMessage1.setUserName(likesRequestDto.getUserName());
           likeMessage1.setTid(likesRequestDto.getTid());
           Date date = new Date();
           SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           likeMessage1.setVotetime(s1.format(date));
           likeMessage1.setStatus(0);
           int i = postService.insertLikeMessage(likeMessage1);
           return CommonResult.success();
       }
   }

   @PostMapping("/getUserAllLikesCount")
    public CommonResult getUserAllLikesCount(@RequestParam("name")String name){
       int count=postService.getUserAllLikesCount(name);
       return CommonResult.success(count);
   }

   @PostMapping("/getDimPost")
    public CommonResult getDimPost(@RequestParam("dimTitle")String dimTitle){
       List<Post> list=postService.getDimPost(dimTitle);
       if(list!=null){
           return CommonResult.success(list);
       }else{
           return CommonResult.fail(ResultEnum.FIND_POST_FAIL);
       }
   }

   @PostMapping("/getUserPostLikeStatus")
    public CommonResult getUserPostLikeStatus(@RequestBody LikesRequestDto likesRequestDto){
       LMessage lMessage=new LMessage();
       lMessage.setUserName(likesRequestDto.getUserName());
       lMessage.setTid(likesRequestDto.getTid());
       LikeMessage likeMessage=postService.getLikeMessage(lMessage);
       if(likeMessage!=null){
           int status=postService.getPostLikeStatus(lMessage);
           return CommonResult.success(status);
       }else{
           return CommonResult.fail(ResultEnum.LIKE_NOT_EXIT);
       }
   }

   @PostMapping("/updatePostLikeStatus")
    public CommonResult updateStatus(@RequestBody UpdateUserLikePostDto updateUserLikePostDto){
       LMessage lMessage=new LMessage();
       lMessage.setUserName(updateUserLikePostDto.getUserName());
       lMessage.setTid(updateUserLikePostDto.getTid());
       LikeMessage likeMessage=postService.getLikeMessage(lMessage);
       if(likeMessage!=null){
           int i=postService.updatePostLikeStatus(updateUserLikePostDto);
           return CommonResult.success();
       }else{
           return CommonResult.fail(ResultEnum.LIKE_NOT_EXIT);
       }
   }

   @PostMapping("/getUserPostCount")
    public CommonResult getUserPostCount(@RequestParam("userName")String userName){
       int postCount=postService.getUserPostCount(userName);
       return CommonResult.success(postCount);
   }

   @PostMapping("/getUserPostCommentCount")
    public CommonResult getUserPostCommentCount(@RequestParam("userName")String userName){
       int postCommentCount=postService.getUserPostCommentCount(userName);
       return CommonResult.success(postCommentCount);
   }

   @PostMapping("/getUserPosts")
    public CommonResult getUserPosts(@RequestParam("userName")String userName){
       List<Post> list=postService.getUserPosts(userName);
       if(list!=null){
           return CommonResult.success(list);
       }else{
           return CommonResult.fail(ResultEnum.NO_POST_EXIST);
       }
   }

   @PostMapping("/deleteUserPost")
    public CommonResult deleteUserPost(@RequestBody PostDeleteRequestDto postDeleteRequestDto){
       LMessage lMessage=new LMessage();
       lMessage.setTid(postDeleteRequestDto.getTid());
       lMessage.setUserName(postDeleteRequestDto.getUserName());
       int i=postService.deleteUserPost(lMessage);
       if(i!=0){
           int j=postService.deletePostComment(postDeleteRequestDto.getTid());
           return CommonResult.success();
       }
       return CommonResult.fail(ResultEnum.DELETE_POST_FAIL);
   }

   @PostMapping("/getPlatePostCount")
    public CommonResult getPlatePostCount(@RequestParam("plate") String plate){
       int platePostCount=postService.getPlatePostCount(plate);
       return CommonResult.success(platePostCount);
   }

   @PostMapping("/getPostLikeSort")
    public CommonResult getPostLikeSort(){
       List<Post> posts=postService.selectAllPost();
       Post temp;
       for(int i=0;i<posts.size();i++){
           for(int j=0;j<posts.size()-i-1;j++){
               if(posts.get(j).getLikes()<posts.get(j+1).getLikes()){
                   temp=posts.get(j);
                   posts.set(j,posts.get(j+1));
                   posts.set(j+1,temp);
               }
           }
       }
       return CommonResult.success(posts);
   }

   @PostMapping("/updatePostAvatar")
    public CommonResult updatePostAvatar(@RequestBody AvatarUrlRequestDto avatarUrlRequestDto){
       int i=postService.updatePostAvatar(avatarUrlRequestDto);
       if(i!=0){
           return CommonResult.success();
       }else{
           return CommonResult.fail(ResultEnum.UPDATE_MESSAGE_FAIL);
       }
   }

   @PostMapping("/updateReplyAvatar")
    public CommonResult updateReplyAvatar(@RequestBody AvatarUrlRequestDto avatarUrlRequestDto){
       int i=postService.updateReplyAvatar(avatarUrlRequestDto);
       if(i!=0){
           return CommonResult.success();
       }else{
           return CommonResult.fail(ResultEnum.UPDATE_MESSAGE_FAIL);
       }
   }








}
